<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Error handling with libxml error handling functions</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="xsl.examples-collection.html">« Example collection.xml and collection.xsl files</a></li>
      <li style="float: right;"><a href="class.xsltprocessor.html">XSLTProcessor »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="xsl.examples.html">范例</a></li>
    <li>Error handling with libxml error handling functions</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="xsl.examples-errors" class="section">
  <h2 class="title">Error handling with libxml error handling functions</h2>
  <p class="para">
   libxml offers a number of functions for handling errors, which can be
   employed to capture and deal with errors in XSLT processing.
  </p>
  <p class="para">
   <div class="example" id="example-5499">
    <p><strong>示例 #1 fruits.xml</strong></p>
    <div class="example-contents"><p>A valid XML file.</p></div>
    <div class="example-contents">
<div class="xmlcode"><pre class="xmlcode">&lt;fruits&gt;
 &lt;fruit&gt;Apple&lt;/fruit&gt;
 &lt;fruit&gt;Banana&lt;/fruit&gt;
 &lt;fruit&gt;Cherry&lt;/fruit&gt;
&lt;/fruits&gt;</pre>
</div>
    </div>

   </div>
   <div class="example" id="example-5500">
    <p><strong>示例 #2 fruits.xsl</strong></p>
    <div class="example-contents"><p>Contains an invalid select expression.</p></div>
    <div class="example-contents">
<div class="xmlcode"><pre class="xmlcode">&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
 &lt;xsl:output method=&quot;html&quot; encoding=&quot;utf-8&quot; indent=&quot;no&quot;/&gt;
 &lt;xsl:template match=&quot;fruits&quot;&gt;
  &lt;ul&gt;
   &lt;xsl:apply-templates/&gt;
  &lt;/ul&gt;
 &lt;/xsl:template&gt;
 &lt;xsl:template match=&quot;fruit&quot;&gt;
  &lt;li&gt;&lt;xsl:value-of select=&quot;THIS IS A DELIBERATE ERROR!&quot;/&gt;&lt;/li&gt;
 &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</pre>
</div>
    </div>

   </div>
   <div class="example" id="xsl.examples-errors.capture">
    <p><strong>示例 #3 Collating and printing errors</strong></p>
    <div class="example-contents"><p>
     The example below captures and displays libxml errors raised when calling
     <span class="methodname"><a href="xsltprocessor.importstylesheet.html" class="methodname">XSLTProcessor::importStyleSheet()</a></span> with a
     stylesheet containing an error.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$xmldoc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$xsldoc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$xsl&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">XSLTProcessor</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$xmldoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruits.xml'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$xsldoc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'fruits.xsl'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">libxml_use_internal_errors</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xsl</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">importStyleSheet</span><span style="color: #007700">(</span><span style="color: #0000BB">$xsldoc</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">libxml_get_errors</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$error</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Libxml&nbsp;error:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$error</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">message</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">libxml_use_internal_errors</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$xsl</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">transformToXML</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmldoc</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Libxml error: Invalid expression

Libxml error: compilation error: file fruits.xsl line 9 element value-of
Libxml error: xsl:value-of : could not compile select expression &#039;THIS IS A DELIBERATE ERROR!&#039;
</pre></div>
    </div>
   </div>
  </p>
 </div></div></div></body></html>